import {
  disposeOrderAudit,
  disposeOrderHandelDetail,
  disposeOrderProcessDetail
} from '@/apis/order.js'
import { Toast } from 'vant'
import { closeAppWebPage } from '@/utils/helper.js'

export const useOderDispose = () => {
  const disposeLoading = ref(false)
  const processLoading = ref(false)
  const auditLoading = ref(false)
  const processTabs = ref()
  const processList = ref([])
  const disposeData = ref({
    province: [],
    city: [],
    country: []
  })
  // 获取工单处置情况数据
  async function getOrderDisposeData(query) {
    try {
      disposeLoading.value = true
      const res = await disposeOrderHandelDetail(query)
      if (res) {
        let { ret, data } = res
        if (ret === 0) {
          disposeData.value.province = data?.province || []
          disposeData.value.city = data?.city || []
          disposeData.value.country = data?.country || []
        }
      }
      disposeLoading.value = false
    } catch (error) {
      console.error(error)
      disposeLoading.value = false
    }
  }

  // 获取工单处置进度数据
  async function getOrderProcessData(query) {
    try {
      processLoading.value = true
      const res = await disposeOrderProcessDetail(query)
      if (res) {
        let { ret, data } = res
        if (ret === 0) {
          processList.value = data?.processList
          processTabs.value = data?.tabs
        }
      }
      processLoading.value = false
    } catch (error) {
      console.error(error)
      processLoading.value = false
    }
  }

  // 上报审核
  async function editDisposeOrderAudit(query, routerOrigin) {
    try {
      auditLoading.value = true
      const res = await disposeOrderAudit(query)
      if (res) {
        let { ret, msg } = res
        if (ret === 0) {
          Toast(msg)
          if (routerOrigin === 'internal') {
            await router.replace('/workOrder')
          } else {
            closeAppWebPage()
          }
        }
      }
      auditLoading.value = false
    } catch (error) {
      console.error(error)
      auditLoading.value = false
    }
  }

  return {
    disposeLoading,
    processLoading,
    auditLoading,
    disposeData,
    getOrderDisposeData,
    getOrderProcessData,
    editDisposeOrderAudit,
    processTabs,
    processList
  }
}
